﻿using System.Collections.Generic;
using Common.Logging;
using Common.Model;
using Common.util;
using Dapper;
using java.lang;
using MoreLinq;
using MySql.Data.MySqlClient;

namespace Common.Dao
{
    public class DadayouziFupanDao
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DadayouziFupanDao));
        
        public static void BatchSave(List<DadayouziFupan> list)
        {
            int batchSize = 100;
            var batchData = list.Batch(batchSize);
            foreach (var data in batchData)
            {
                var sql =
                    new StringBuilder("INSERT IGNORE tb_dadayouzi_wechat_fupan (`title`,`site_time`,`crawling_time`,`content`,`author`) VALUES ");
                bool first = true;
                foreach (DadayouziFupan fupan in data)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sql.append(",");
                    }
                    sql.append(
                        $"('{MySqlHelper.DoubleQuoteString(fupan.Title)}'" +
                        $",'{fupan.SiteTime}'" +
                        $",'{fupan.CrawlingTime}'" +
                        $",'{MySqlHelper.DoubleQuoteString(fupan.Content)}'" +
                        $",'{MySqlHelper.DoubleQuoteString(fupan.Author)}')");
                }

                using (var cnn = DbUtils.GetHotTopicConnection())
                {
                    cnn.Execute(sql.ToString());
                }
            }
        }
    }
}